﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using BLL;
using DTO;

namespace CRM.GUI.QLHeThong
{
    public partial class ucRegion : UserControl
    {
        private CallManagement cm = new CallManagement();
        private BLsupervisor sup = new BLsupervisor();
        private Administrators ad = new Administrators();
        cDataInTransfer data = new cDataInTransfer();
        private int addcase = 0;
        private int expand = 0;
        private int index = -1;
        private RegionEntity _Region;
        private List<RegionEntity> _list;

        public ucRegion()
        {
            InitializeComponent();
            LoadIntial();
            sptRegion.Panel2Collapsed = true;
            treeRegion.BestFitColumns();
        }

        private void LoadIntial()
        {
            _list = data.LoadRegionList();
            treeRegion.DataSource = _list;
            treeRegion.KeyFieldName = "Region_Code";
            treeRegion.ParentFieldName = "Parent_Code";

            cmbRootRegion.Properties.DataSource = data.LoadRegionList();
            cmbRootRegion.Properties.DisplayMember = "RegionName";
            cmbRootRegion.Properties.ValueMember = "Region_Code";
        }

        private void mbtnAddRoot_Click(object sender, EventArgs e)
        {
            addcase = 1;
            sptRegion.Panel2Collapsed = false;
            cmbRegionLvl.SelectedIndex = 0;
            txtNewRegion.Focus();
        }

        private void mbtnAddChild_Click(object sender, EventArgs e)
        {
            addcase = 2;
            grbRegion.Text = "Thêm mục địa lý con";
            sptRegion.Panel2Collapsed = false;
            cmbRegionLvl.SelectedIndex = _Region.RegionLevel;
            cmbRootRegion.EditValue = _Region.Region_Code;
            txtNewRegion.Focus();
        }

        private void mbtnUpdateNode_Click(object sender, EventArgs e)
        {
            addcase = 3;
            grbRegion.Text = "Cập nhật địa lý";
            sptRegion.Panel2Collapsed = false;
            cmbRootRegion.Enabled = true;
            cmbRegionLvl.SelectedIndex = _Region.RegionLevel - 1;
            cmbRootRegion.EditValue = _Region.Region_Code;
            txtNewRegion.Focus();
        }

        private void mbtnExpand_CheckStateChanged(object sender, EventArgs e)
        {
            if (mbtnExpand.CheckState == CheckState.Checked)
            {
                treeRegion.ExpandAll();
                expand = 1;
                mbtnCollapse.CheckState = CheckState.Unchecked;
            }
        }

        private void mbtnCollapse_CheckStateChanged(object sender, EventArgs e)
        {
            if (mbtnCollapse.CheckState == CheckState.Checked)
            {
                treeRegion.CollapseAll();
                expand = 0;
                mbtnExpand.CheckState = CheckState.Unchecked;
            }
        }

        private void CheckInput()
        {
            if (string.IsNullOrWhiteSpace(txtNewRegion.Text))
            {
                MessageBox.Show("Tên địa lý không được để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                addcase = 0;
                return;
            }        
        }

        private void btnAddRegion_Click(object sender, EventArgs e)
        {
            CheckInput();
            RegionEntity re = new RegionEntity();
            string errmessage = "Có lỗi xảy ra trong quá trình thực hiện\nXin vui lòng thực hiện lại.";
            switch (addcase)
            { 
                case 0:
                    return;
                case 1:
                    re.RegionName = txtNewRegion.Text;
                    re.RegionType = cmbRegionLvl.SelectedItem.ToString();
                    re.RegionLevel = 1;
                    re.Ordinal = SharedData.RegionList.Where(a => a.RegionLevel == 1).ToList().Count + 1;
                    re.Parent_Code = 0;
                    try
                    {
                        ad.AddNewRegion(re);
                        MessageBox.Show("Thêm mới thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch
                    {
                        MessageBox.Show(errmessage, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    break;
                case 2:
                    re.RegionName = txtNewRegion.Text;
                    re.RegionType = cmbRegionLvl.SelectedItem.ToString();
                    re.RegionLevel = _Region.RegionLevel + 1;
                    re.Ordinal = SharedData.RegionList.Where(a => a.RegionLevel == _Region.RegionLevel + 1).ToList().Count + 1;
                    re.Parent_Code = _Region.Region_Code;
                    try
                    {
                        ad.AddNewRegion(re);
                        MessageBox.Show("Thêm mới thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch
                    { 
                        MessageBox.Show(errmessage, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    break;
                case 3:                    
                    re.Region_Code = _Region.Region_Code;
                    re.RegionName = txtNewRegion.Text;
                    re.RegionType = cmbRegionLvl.SelectedItem.ToString();
                    if (cmbRootRegion.EditValue != null && ((RegionEntity)cmbRootRegion.EditValue).Region_Code != _Region.Region_Code)
                    {
                        var regiontmp = (RegionEntity)cmbRootRegion.EditValue;
                        re.Parent_Code = regiontmp.Region_Code;
                        re.RegionLevel = regiontmp.RegionLevel + 1;
                        re.Ordinal = SharedData.RegionList.Where(a => a.RegionLevel == regiontmp.RegionLevel).ToList().Count + 1;
                    }
                    else
                    {
                        re.Parent_Code = _Region.Parent_Code;
                        re.Ordinal = _Region.Ordinal;
                        re.RegionLevel = _Region.RegionLevel;
                    }
                    try
                    {
                        ad.AddNewRegion(re);
                        MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch
                    { 
                        MessageBox.Show(errmessage, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    break;
            }

            sptRegion.Panel2Collapsed = true;
            txtNewRegion.Text = null;
            cmbRegionLvl.SelectedItem = null;
            cmbRootRegion.EditValue = null;
            _list = data.LoadRegionList();
            treeRegion.DataSource = _list;
            SharedData.RegionList = _list;
            cmbRootRegion.Properties.DataSource = _list;
            treeRegion.FocusedNode = treeRegion.FindNodeByID(index);
            if (expand == 1)
            {
                treeRegion.ExpandAll();
            }
            else
            {
                treeRegion.CollapseAll();
            }
        }

        private void treeRegion_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DevExpress.XtraTreeList.TreeList tree = sender as DevExpress.XtraTreeList.TreeList;
                DevExpress.XtraTreeList.TreeListHitInfo hitInfo = tree.CalcHitInfo(e.Location);
                index = hitInfo.Node.Id;
                _Region = (RegionEntity)treeRegion.GetDataRecordByNode(hitInfo.Node);
                if (_Region == null)
                {
                    mbtnAddChild.Enabled = false;
                    mbtnUpdateNode.Enabled = false;
                }
                else if (_Region != null && _Region.RegionLevel != 4)
                {
                    mbtnAddChild.Enabled = true;
                    mbtnUpdateNode.Enabled = true;
                }
                else if (_Region != null && _Region.RegionLevel == 4)
                {
                    mbtnAddChild.Enabled = false;
                    mbtnUpdateNode.Enabled = false;
                }

            }
        }

        private void treeRegion_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            _Region = (RegionEntity)treeRegion.GetDataRecordByNode(e.Node);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            sptRegion.Panel2Collapsed = true;
            cmbRegionLvl.SelectedItem = null;
            cmbRootRegion.EditValue = null;
            txtNewRegion.Text = null;
        }

    }
}
